﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Templates;

namespace RibbonEditor.Controls.Templates
{
    public partial class ControlRefControl : UserControl, IControlRulable
    {
        private ControlRef innerControlRef;

        public ControlRef InnerControlRef
        {
            get { return innerControlRef; }
            set { innerControlRef = value; }
        }

        public ControlRefControl()
        {
            InitializeComponent();

            this.ecDisplayMode.Values = Enum.GetValues(typeof(Enumerations.DisplayMode));

            this.innerControlRef = new ControlRef();
            this.FillControls();
        }

        public ControlRefControl(ControlRef controlRef)
        {
            this.innerControlRef = controlRef;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.ecDisplayMode.SelectedValue = this.innerControlRef.DisplayMode.Value.ToString();
                this.strTemplateAlias.Value = this.innerControlRef.TemplateAlias;
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerControlRef.DisplayMode = (Enumerations.DisplayMode)Enum.Parse(typeof(Enumerations.DisplayMode), this.ecDisplayMode.SelectedValue);
                    this.innerControlRef.TemplateAlias = this.strTemplateAlias.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ControlRefControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerControlRef.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
